package fr.LesLoutresSauvages.laloutresportive.util;

import org.json.JSONException;
import org.json.JSONObject;

public class Competition implements JsonConvertible
{

  private int id;
  private String nom;
  private Sport s;

  
  
  
  
  public Competition()
  {
    super();
    this.id = 0;
    this.nom = "";
    this.s = new Sport();
  }

  public Competition(int id, String nom, Sport s)
  {
    super();
    this.id = id;
    this.nom = nom;
    this.s = s;
  }

  
  
  
  
  public int getId()
  {
    return id;
  }

  public void setId(int id)
  {
    this.id = id;
  }

  public String getNom()
  {
    return nom;
  }

  public void setNom(String nom)
  {
    this.nom = nom;
  }

  public Sport getSport()
  {
    return s;
  }

  public void setSport(Sport s)
  {
    this.s = s;
  }

  
  
  
  
  @Override
  public String toString()
  {
    return "Competition [id=" + id + ", nom=" + nom + ", s=" + s.toString() + "]";
  }

  
  
  
  
  
  @Override
  public String toJSON()
  {
    JSONObject jObj = new JSONObject();
    try
    {
      jObj.put("id", this.getId());
      jObj.put("nom", this.getNom());
      jObj.put("sport", this.getSport().toJSON()); // Genere des '\' avant les guillemets et genere des guillemets autour du JSON incruste. Le fromJSON() le decrypte correctement, a tester avec le serveur du concour.
    }
    catch (JSONException e)
    {
      e.printStackTrace();
    }
    
    return ( jObj.toString() );
  }

  @Override
  public void fromJSON(String str)
  {
    try
    {
      JSONObject jObj = new JSONObject(str);
      this.setId(jObj.getInt("id"));
      this.setNom(jObj.getString("nom"));
      Sport s = new Sport();
      s.fromJSON(jObj.getString("sport"));
      this.setSport(s);
    }
    catch (JSONException e)
    {
      e.printStackTrace();
    }
  }
}
